<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="./bootstrap/css/bootstrap.css">
    <link rel="stylesheet" href="./css/base.css">
    <style>
        .crumbs {
            font-size: 14px;
            color: #ccc;
        }
        #artcontent img{
            max-width: 500px;
            max-height: 500px;

        }
        #artheat img{
            max-width: 500px;
            max-height: 500px;

        }
    </style>
</head>

<body>
    <div class="container">
        <nav class="navbar navbar-default">
            <div class="container-fluid">
                <!-- Brand and toggle get grouped for better mobile display -->
                <div class="navbar-header">
                    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                        data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="#" style="display:flex;">
                        Allen的博客
                    </a>
                </div>

                <!-- Collect the nav links, forms, and other content for toggling -->
                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">

                    <ul class="nav navbar-nav navbar-right">
                        <li><a href="#">首页</a></li>
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
                                aria-haspopup="true" aria-expanded="false">分类 <span class="caret"></span></a>
                            <ul class="dropdown-menu">
                                <li><a href="/cate.html?id=2">html</a></li>
                                <li><a href="/cate.html?id=2">css</a></li>
                                <li><a href="/cate.html?id=2">javascript</a></li>
                                <li role="separator" class="divider"></li>
                                <li><a href="/cate.html?id=2">mysql</a></li>
                                <li><a href="/cate.html?id=2">Linux</a></li>
                            </ul>
                        </li>
                        <li><a href="/tags.html">标签</a></li>
                        <li><a href="/about.html">关于我</a></li>
                    </ul>
                </div><!-- /.navbar-collapse -->
            </div><!-- /.container-fluid -->
        </nav>
        <div style="text-align:center;" id="artheat">

        </div>
        <div class="content" id="artcontent">

        </div>
        <hr>
        <div class="footer" style="text-align:center;font-size: 14px; color: #999;margin:10px 0;">
            首页 | 主站 | GitHub | Powered by hexo | Theme xoxo
        </div>
    </div>
</body>
<script src="./js/jquery.js"></script>
<script src="./bootstrap/js/bootstrap.min.js"></script>
<script>
    let imgs=document.querySelectorAll('img')
    let concent=true
    scrollHandle(imgs)  
    let  urldata= location.search.match(/\?(id=(\d+))/g)
    urldata ? urldata= urldata[0].substring(4) :location.href="/"
    console.log(urldata)
    if(urldata==""  || !urldata){
    location.href="/"
    connect =false;
}
    function loadartconten() {
        return $.get('http://127.0.0.1:3200/api/getidArt?id=' + urldata)
    }

    concent ?  loadartconten().then(res => {
        console.log(res)
        let footart = `
         <h4 class="crumbs" style="text-align:left;">当前位置：首页 / ${res.classify}</h4>
            <h3> ${res.title}</h3>
            <div style="font-size: 14px; color: #999;">  ${res.add_time.substring(0, 10)} /
                分类： ${res.classify} / 点赞：888 <span class="label label-default">HTML</span> <span class="label label-default">JS</span>
            </div>
            <p class="intro">
              ${res.title}
            </p>
            <img data-src="http://127.0.0.1:3200/${res.img_url}" alt="">      
         `
        $("#artheat").html(footart)
        $("#artcontent").html(res.content)
        imgs=document.querySelectorAll('img')
		scrollHandle(imgs)
    })  :  null

    function renderCate(data) {
        catacontent = "";
        data.forEach(item => {
            catacontent += `	<li><a href="/cate.html?id=${item.article_id}">${item.classify}</a></li>`
        });

        return catacontent
    }
    function loadcate() {
        return $.get('http://127.0.0.1:3200/api/cate')
    }
    let getcate = localStorage.getItem("cate")
    getcate ? getcate = JSON.parse(getcate) : loadcate().then(res => getcate = res);
    renderCate(getcate)
    $(".dropdown-menu").html(renderCate(getcate))
    imgs=document.querySelectorAll('img')
		scrollHandle(imgs)

    //图片懒加载
    function scrollHandle(imgs) {
        // let pageScrollTop = document.documentElement.scrollTop || document.body.scrollTop  //向上卷的长度
        let viewportHeight = window.innerHeight || document.documentElement.clientHeight;  //视口高度
        imgs.forEach(img => {

            let { top } = img.getBoundingClientRect();

            if (top > 0 && top - 100 < viewportHeight) {

                if (img.getAttribute('loader') !== 'done') {
                    let src = img.getAttribute('data-src')
                    img.src = src
                    img.setAttribute('loader', 'done')
                }

            }
        })
    }


</script>

</html>